import { PageDto } from "../models/page.model";
import { UaTradeEntity, UaTradeSearchDto } from "../models/ua-trade.model";
import { GlobalService } from "./global.service";
import { LocalStorage } from "./local-storage.service";


export class UaTradeService {
  static PATH = "/ua/trade";
  static searchWithPage(data: UaTradeSearchDto, page: number) {
    return new Promise<PageDto<UaTradeEntity>>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaTradeService.PATH}/search?page=${page}`,
          method: "POST",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as PageDto<UaTradeEntity>),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static findById(id: number) {
    return new Promise<UaTradeEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaTradeService.PATH}/${id}`,
          method: "GET",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as UaTradeEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static hot(id: number) {
    return new Promise<boolean>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaTradeService.PATH}/hot/${id}`,
          method: "GET",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as any),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static removeById(id: number) {
    return new Promise<UaTradeEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaTradeService.PATH}/${id}`,
          method: "DELETE",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          success: (response) => resolve(response.data as UaTradeEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static createTrade(data: UaTradeEntity) {
    return new Promise<UaTradeEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaTradeService.PATH}/`,
          method: "POST",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as UaTradeEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }

  static updateTrade(data: UaTradeEntity) {
    return new Promise<UaTradeEntity>(
      (resolve, reject) => {
        wx.request({
          url: `${GlobalService.HOST}${UaTradeService.PATH}/`,
          method: "PUT",
          header: {
            'openId': LocalStorage.getUserInfo()?.openId,
          },
          data,
          success: (response) => resolve(response.data as UaTradeEntity),
          fail: (err) => reject(err)
        });
      }
    )
  }
}
